import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store/index.js';
// 主页+登录注册
import HomeView from '@/views/HomeView.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
// 用户
import Library from '@/home/Library.vue'
import User from '@/home/User.vue'
import Cart from '@/home/Cart.vue'
import Shop from '@/home/Shop.vue'
import Detail from '@/home/GoodDetail.vue'
import ChatRoom from '@/home/ChatRoom.vue'
import MusicBox from '@/home/MusicBox.vue'
// 管理员
import DevOps from '@/home/admin/DevOps.vue';
import Examine from '@/home/admin/Examine.vue';
import UserManager from '@/home/admin/UserManager.vue';
// 开发者
import DataAnalysis from '@/home/developer/DataAnalysis.vue';
import GameManager from '@/home/developer/GameManager.vue';
import GameUpload from '@/home/developer/GameUpload.vue';

const {ipcRenderer} = require('electron')
const routes = [
  {
    path: '/',
    name: 'login',
    component: Login,
  },
  {
    path: '/reg',
    name: 'reg',
    component: Register,
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      { path: '/home', redirect: 'home/library',},
      // 用户
      {
        path: 'user',
        component: User,
      },
      { path: 'library', 
        component: Library, 
      }, 
      {
        path: 'cart',
        component: Cart,
      },
      {
        path: 'shop',
        component: Shop,
      },
      {
        path: 'detail',
        component: Detail,
      },
      {
        path: 'chat',
        component: ChatRoom,
      },
      {
        path: 'music',
        component: MusicBox,
      },
      // 管理员
      {
        path: 'devops',
        component: DevOps,
      },
      {
        path: 'examine',
        component: Examine,
      },
      {
        path: 'usermanager',
        component: UserManager,
      },
      // 开发者
      {
        path: 'analysis',
        component: DataAnalysis,
      },
      {
        path: 'gamemanager',
        component: GameManager,
      },
      {
        path: 'gameupload',
        component: GameUpload,
      }
    ]
  },
]
// 记录窗口大小的数据的数组
const sizeArr = {'/':{width: 720, height: 420, path: 'logreg'},'/reg':{width: 820, height: 720, path: 'logreg'},'/home':{width: 1080, height: 700, path: 'home'}}

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// async+await设置同步，axios默认是异步会导致页面切换和验证不同步，出现问题
router.beforeEach(async (to, from, next) => {
  // 区分跳转的窗口改变窗口大小，to.path可以获取路径
  // console.log(to.path);
  // console.log(to.query);
  console.log(`to.path:${to.path},from.path:${from.path}`);
  const identity = store.state.identity;
  // 从登录页面进入库页面特殊处理
  // if(to.path=="/home/library") store.commit('changeView',2);
  // 改变页面的导航指示
  if(identity==0&&to.query.id){
    store.commit('changeView',to.query.id);
  }else if(identity==1&&to.query.id){
    store.commit('changeView',to.query.id-6);
  }else if(identity==2&&to.query.id){
    store.commit('changeView',to.query.id-9);
  }
  if((from.path=='/'||from.path=='/reg')&&to.path.includes('home')){
    ipcRenderer.send('changeWindowSize', sizeArr['/home'])
  }else if(to.path=='/'){
    console.log('前往登录');
    ipcRenderer.send('changeWindowSize', sizeArr['/'])
  }else if(to.path=='/reg'){
    ipcRenderer.send('changeWindowSize', sizeArr['/reg'])
  }
  next();

})
export default router
